package github.kawaiior.yuanchuxiuzhen.common.world.biomes;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import github.kawaiior.yuanchuxiuzhen.core.init.BiomeInit;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryLookupCodec;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.Biomes;
import net.minecraft.world.biome.provider.BiomeProvider;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class HellBiomeProvider extends BiomeProvider {

    public static final Codec<HellBiomeProvider> HELL_CODEC = RecordCodecBuilder.create(instance ->
            instance.group(
                    Codec.LONG.fieldOf("seed").forGetter(provider -> provider.seed),
                    RegistryLookupCodec.getLookUpCodec(Registry.BIOME_KEY).forGetter(provider -> provider.registry)
            ).apply(instance, HellBiomeProvider::new)
    );

    private final List<Biome> biomes;
    private final long seed;
    private final Registry<Biome> registry;

    public HellBiomeProvider(long seed, Registry<Biome> registry) {
        super(getStaticBiomes(registry));
        this.seed = seed;
        this.registry = registry;
        this.biomes = getStaticBiomes(registry);
    }

    private static List<Biome> getStaticBiomes(Registry<Biome> registry) {
        // 在这里配置你的生物群系
        return Arrays.asList(
                registry.getOrThrow(BiomeInit.HEAVEN)
        );
    }

    @Override
    protected Codec<? extends BiomeProvider> getBiomeProviderCodec() {
        return HELL_CODEC;
    }

    @Override
    public BiomeProvider getBiomeProvider(long seed) {
        return new HellBiomeProvider(seed, this.registry);
    }

    @Override
    public Biome getNoiseBiome(int x, int y, int z) {
        Random rand = new Random(seed ^ (x * 31 + y * 17 + z * 37));
        return biomes.get(rand.nextInt(biomes.size()));
    }
}